package com.learn.netty.writeAndFlush;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * 负责客户端的数据处理，并通过调用 {@link io.netty.channel.Channel#writeAndFlush(Object)}
 * 向客户端返回 {@link ResponseSample} 对象，其中包含返回码、响应数据以及时间戳。
 */
public class RequestSampleHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        String data = ((ByteBuf) msg).toString(CharsetUtil.UTF_8);
        ResponseSample response = new ResponseSample("OK", data, System.currentTimeMillis());
        ctx.channel().writeAndFlush(response);
    }

}
